/****************************************Copyright (c)**********************************************
**                               		特飞科技公司
**
* 文    件 : 16-channel, 12-bit PWM Fm+ I2C-bus LED controller PAC9685驱动头文件
* 作    者 : 李海洋
* 版    本 ：v1.0
* 完成时间 : 2023年6月24日
* 描    述 ：**********使用步骤:*************
*			1#使用该驱动时应在Includes.h中包含该头文件, 且位于配置定义或Config.h头文件之后!
*			  需要在配置头文件或配置定义定义以下配置宏定义
*			  #define USE_SPI_NUM		1	//定义使用SPI数量
*			  #define NO_USE_FREE_RTOS	//如果不使用操作系统需要明确给出定义
*			2#在c文档中定义SPI驱动对象的结构指针
*			  sSPI_DRIVER *pXXX;
*			3#调用SPI_Init给pXXX赋值
*			  SPI_Init(&pXXX, &spi_handle, CpltSemHandle);
*			4#使能片选信号然后根据需要分别调用以下3个方法. 这些方法属于同步调用
*			  SPI_Only_Tx, SPI_Only_Rx, SPI_Tx_Rx
**--------------------------------------------------------------------------------------------------
* 修 改 人 ：
* 版    本 ：
* 修改时间 : 年月日
* 修改内容 : 
****************************************************************************************************
*/
#pragma once

#include "I2C_driver.h"

#define PAC9685_ADDR		0xAA		//硬件地址
#define ALLCALL_ADDR		0xE0		//群呼地址
#define SWRST_ADDR			0x00		//软复位地址
#define SWRST_CMD			0x06		//软复位命令

#pragma region //===PAC9685寄存器定义===
//'*'--表示默认值
enum MODE1_BIT
{
	ALLCALL = (1<<0),	//[R/W]1*=PCA9685 responds to LED All Call I2C-bus address. 0=not.
	SUB3 = (1<<1),		//[R/W]1=PCA9685 responds to I2C-bus subaddress 3. 0*=not.
	SUB2 = (1<<2),		//[R/W]1=PCA9685 responds to I2C-bus subaddress 2. 0*=not.
	SUB1 = (1<<3),		//[R/W]1=PCA9685 responds to I2C-bus subaddress 1. 0*=not.
	SLEEP = (1<<4),		//[R/W]1*=Low power mode. Oscillator off. 0=Normal mode(1->0 takes 500us)
	AI = (1<<5),		//[R/W]1=Register Auto-Increment enabled. 0*=disable.
	EXTCLK = (1<<6),	//[R/W]1=Use EXTCLK pin clock. 0*=Use internal clock.
	RESTART = (1<<7)	//[R/WC]1=Restart enabled. 0*=Restart disabled.
};
enum MODE2_BIT
{
	OUTNE = (3<<0),		//[R/W]00*=When OE = 1 (output drivers not enabled), LEDn = 0.
	//01=When OE = 1 : LEDn = 1 when OUTDRV = 1, LEDn = high-impedance when OUTDRV = 0
	//1X=When OE = 1 : LEDn = high-impedance.
	OUTDRV = (1<<2),	//[R/W]1*=The 16 LEDn outputs are configured with a totem pole structure. 
	//0=The 16 LEDn outputs are configured with an open-drain structure.
	OCH = (1<<3),		//[R/W]0*=Outputs change on STOP command. 1=Outputs change on ACK.
	INVRT = (1<<4),	//[R/W]1=Output logic state inverted. Value to use when no external driver used.Applicable when OE = 0.
	//0*=Output logic state not inverted. Value to use when external driver used. Applicable when OE = 0.
};
#define LED_FULL_ON		4	//[R/W]1=PWM100%开, 位于LEDn_ON_H中 和[W]1*在ALL_LED_ON_H中
#define LED_FULL_OFF	4	//[R/W]1*=PWM100%关, 位于LEDn_OFF_H中 和[W]1*在ALL_LED_OFF_H中

#pragma pack(1)
//PAC9685寄存器定义
typedef struct
{
	uint8_t MODE[2];	//[R/W]Mode register
	uint8_t SUBADR[3];	//[R/W]I2C-bus subaddress
	uint8_t ALLCALLADR; //[R/W]LED All Call I2C-bus address
	struct
	{
		uint16_t LED_ON; //[R/W]LEDi output and brightness control
		uint16_t LED_OFF; //[R/W]LEDi output and brightness control
	} BR_Ctrl[16];	//亮度控制
	uint8_t res[180];	//保留
	uint16_t ALL_LED_ON; //[R/W]load all the LEDn_ON registers
	uint16_t ALL_LED_OFF; //[R/W]load all the LEDn_OFF registers
	uint8_t PRE_SCALE; //[R/W]prescaler for PWM output frequency=25M/(4096*200Hz)-1=30(0x1E):1526Hz(0x03)--24Hz(0xFF)
	uint8_t TestMode;
} PAC9685_Reg_t;
#pragma pack()

#define PAC9685_Reg		((PAC9685_Reg_t*)0)

#pragma endregion

#pragma region //===方法===
/*
********************************************************************************
 *描述: 初始化PAC9685
 ******************************************************************************/
void PAC9685_Init(void *handle/*[I]指向外设句柄*/,
	osSemaphoreId DoneSemId/*[I]I2C通讯完成信号量句柄*/);
/*
********************************************************************************
 *描述: 向PAC9685的某个通道写入新的脉宽数据
 ******************************************************************************/
void PAC_WrPWM(uint8_t channel/*[I]0~15通道之一*/, uint16_t pwm/*[I]新的脉宽数据0~4095*/);


#pragma endregion
